package com.service.doctor.impl;

import com.dao.doctor.PatientMapper;
import com.dao.share.ExcelMapper;
import com.entity.Answer;
import com.entity.patient.Case;
import com.entity.patient.Needs;
import com.entity.patient.Patient;
import com.entity.share.Excel;
import com.service.doctor.PatientService;
import com.util.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    PatientMapper patientMapper;
    @Autowired
    ExcelMapper excelMapper;
    public Object addPatiemt(Patient patient) {
        Answer answer = new Answer();
        try {
            if (patientMapper.addPatiemt(patient) == 1) {
                System.out.println(patient);
                Case casee = new Case();
                casee.setPatientID(patient.getPatientID());
                casee.setDoctorID(patient.getDoctorID());
                patientMapper.addCase(casee);
            } else {
                answer.setCode(0);
                answer.setError("添加失败！");
            }
        } catch (Exception e) {
            answer.setCode(0);
            answer.setError("添加失败！");

            e.printStackTrace();
            return answer;
        }
        answer.setCode(1);
        return answer;
    }

    public Object queryCaseState(String state, int doctorID) {
        Answer answer = new Answer();
        List<Integer> caseStates = new ArrayList<Integer>();
        if (state == null) {
            answer.setCode(0);
            answer.setError("参数为空！");
            return answer;
        }
        caseStates.add(patientMapper.queryCaseStateByState("pending", doctorID));
        caseStates.add(patientMapper.queryCaseStateByState("dealing", doctorID));
        caseStates.add(patientMapper.queryCaseStateByState("processing", doctorID));
        caseStates.add(patientMapper.queryCaseStateByState("over", doctorID));

        //System.out.println(caseStates);
        answer.setCode(1);
        answer.setData(JSONObject.fromObject("{\"patients\":" + JSONArray.fromObject(patientMapper.selectPatienstByState(state, doctorID)) + ",\"caseStates\":" + JSONArray.fromObject(caseStates) + "}"));
        return answer;
    }

    @Transactional
    public Object deleteCaseAllInformation(int patientID, int doctorID) {
        Answer answer = new Answer();
        try {
            if (patientMapper.deletePatient(patientID, doctorID) != 1 || patientMapper.deleteCase(patientID) != 1) {
                answer.setCode(0);
                answer.setError("删除失败！");
                return answer;
            }
        } catch (Exception e) {
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("删除失败！");
            return answer;
        }
        answer.setCode(1);
        return answer;
    }

    @Transactional
    public Object addNeeds(Needs needs) {
        Answer answer = new Answer();
        try {
            needs.setDate(Util.getDate());
            if (patientMapper.addNeeds(needs) != 1 || patientMapper.updatCaseState(needs.getCaseID(), "dealing") != 1) {
                answer.setCode(0);
                answer.setError("添加失败！");
                return answer;
            }
        } catch (Exception e) {
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("添加失败！");
            return answer;
        }
        answer.setCode(1);
        return answer;
    }

    @Transactional
    public Object  seekExperts(int caseID, int expertID,int doctorID,Needs needs){
        Answer answer = new Answer();
        needs.setDate(Util.getDate());
        needs.setCaseID(caseID);
        needs.setState("toAccept");
        try {
//            patientMapper.addAdvisorys(caseID,doctorID,expertID);
            //添加需求
            patientMapper.addNeeds(needs);
            //在需求中添加专家
            patientMapper.needAddExpert(expertID,caseID);
            //改变病例状态
            patientMapper.updatCaseState(caseID,"dealing");


        }catch (Exception e){
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("请求错误！");
            return answer;
        }
        answer.setCode(1);
        return answer;
    }
    public Object queryPatientDetails(Integer patientID){
        Answer answer = new Answer();
        try {
            Patient patient = patientMapper.queryPatient(patientID);
            Excel excel = excelMapper.queryExcel(patientID);
            if (excel != null){
                String data = "{\"caseInfo\":"+JSONObject.fromObject(patient)+"}";
                if (excel.getConclusion() !=null){
                    data = data.substring(0, data.length()-1) +",\"conclusion\":{"+excel.getConclusion().substring(1,excel.getConclusion().length())+",\"patientFormQuestion\":\""+excel.getPatientFormQuestion()+"\",\"patientFormProgram\": \""+excel.getPatientFormProgram()+"\"}";

                }else {
                    data = data.substring(0, data.length()-1) +",\"conclusion\":"+"\"\""+",\"patientFormQuestion\":\""+excel.getPatientFormQuestion()+"\",\"patientFormProgram\": \""+excel.getPatientFormProgram()+"\"}";
                }
                //JSONObject json = JSONObject.fromObject(data);
//                Map<String,Object> map = new HashMap<String,Object>();
//                map.put("caseInfo",patient);
//                map.put("conclusion",excel.getConclusion());
//                map.put("patientFormQuestion",excel.getPatientFormQuestion());
//                map.put("patientFormProgram",excel.getPatientFormProgram());
                answer.setCode(1);
                answer.setData(data);
            }else {
                String data = "{\"caseInfo\":"+JSONObject.fromObject(patient)+"}";
                answer.setCode(1);
                answer.setData(data);
            }

        }catch (Exception e){
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("发生错误");
            return answer;
        }
        return answer;
    }
}
